#include "pch.hpp"
#include "leave.hpp"

static sf::Clock score_anim_timer;
const int LEAVE_COUNT = 10;

//extern std::vector<CLeave> vLeaves;


void drawScore(int score, int width, int leftBorder)
{
	std::vector<int> dig;

	int digit, pos;

	if(score == 0)
		dig.push_back(0);
	else
		while(score > 0)
		{
			digit = score % 10;
			dig.insert(dig.begin(),digit);
			score /= 10;
		}
	
	sf::Vector2f disp(5,5);
	disp *= sin(score_anim_timer.GetElapsedTime()*PI);

	score_spr.SetPosition(sf::Vector2f(leftBorder + 10,15)+disp);
	app.Draw(score_spr);

	pos = (leftBorder + 10) + (score_spr.GetImage()->GetWidth() - (dig.size() * (width - 20) + 20)) / 2;

	for(int i = 0; i < dig.size(); ++i)
	{
		digits[dig[i]].SetPosition(sf::Vector2f(pos + (i * (width - 20)),40)+disp);
		app.Draw(digits[dig[i]]);
	}
}

void drawLevel(int level, int width, int rightBorder)
{
	std::vector<int> dig;

	int digit, pos;

	if(level == 0)
		dig.push_back(0);
	else
		while(level > 0)
		{
			digit = level % 10;
			dig.insert(dig.begin(),digit);
			level /= 10;
		}
	
	sf::Vector2f disp(-5,5);
	disp *= sin(score_anim_timer.GetElapsedTime()*PI);

	level_spr.SetPosition(sf::Vector2f(rightBorder - 10 - level_spr.GetImage()->GetWidth(),15)+disp);
	app.Draw(level_spr);

	pos = (rightBorder - 10 - level_spr.GetImage()->GetWidth())
		+ (level_spr.GetImage()->GetWidth() - (dig.size() * (width - 20) + 20)) / 2;

	for(int i = 0; i < dig.size(); ++i)
	{
		digits[dig[i]].SetPosition(sf::Vector2f(pos + (i * (width - 20)),40)+disp);
		app.Draw(digits[dig[i]]);
	}
}

bool setupMusic()
{	
	/*if (!Music.OpenFromFile("data/music/background.ogg"))
		return false;

    Music.SetLoop(true);
    Music.Play();

	Sounds.push_back(std::pair<sf::SoundBuffer,std::string>(sf::SoundBuffer(),"click"));
	if (!Sounds.back().first.LoadFromFile("data/music/click.ogg")) return false;
	Sounds.push_back(std::pair<sf::SoundBuffer,std::string>(sf::SoundBuffer(),"snakeKill"));
	if (!Sounds.back().first.LoadFromFile("data/music/snakeKill.ogg")) return false;
	Sounds.push_back(std::pair<sf::SoundBuffer,std::string>(sf::SoundBuffer(),"hamsterDies"));
	if (!Sounds.back().first.LoadFromFile("data/music/hamsterDies.ogg")) return false;
	Sounds.push_back(std::pair<sf::SoundBuffer,std::string>(sf::SoundBuffer(),"hamsterHit"));
	if (!Sounds.back().first.LoadFromFile("data/music/hamsterHit.ogg")) return false;
	Sounds.push_back(std::pair<sf::SoundBuffer,std::string>(sf::SoundBuffer(),"addBall"));
	if (!Sounds.back().first.LoadFromFile("data/music/addBall.ogg")) return false;
	Sounds.push_back(std::pair<sf::SoundBuffer,std::string>(sf::SoundBuffer(),"eatBall"));
	if (!Sounds.back().first.LoadFromFile("data/music/eatBall.ogg")) return false;
	Sounds.push_back(std::pair<sf::SoundBuffer,std::string>(sf::SoundBuffer(),"explosion"));
	if (!Sounds.back().first.LoadFromFile("data/music/explosion.ogg")) return false;
	Sounds.push_back(std::pair<sf::SoundBuffer,std::string>(sf::SoundBuffer(),"freeze"));
	if (!Sounds.back().first.LoadFromFile("data/music/freeze.ogg")) return false;
	Sounds.push_back(std::pair<sf::SoundBuffer,std::string>(sf::SoundBuffer(),"slow"));
	if (!Sounds.back().first.LoadFromFile("data/music/slow.ogg")) return false;


	Players.resize(Sounds.size());
*/
	return true;
}

void playSound(std::string name)
{
	/*int i;

	std::vector<std::pair<sf::SoundBuffer,std::string> >::iterator it;
	for (i = 0; i < Sounds.size(); ++i)
		if (Sounds[i].second == name)
			break;

	Players[i].SetBuffer(Sounds[i].first);
	Players[i].Play();*/
}

bool mouseOver(const sf::Vector2f Position, sf::Sprite Sprite)
{
	return  (Position.x < Sprite.GetPosition().x + Sprite.GetSize().x / 2.0f) && (Sprite.GetPosition().x - Sprite.GetSize().x / 2.0f < Position.x) &&
         (Position.y < Sprite.GetPosition().y + Sprite.GetSize().y / 2.0f) && (Sprite.GetPosition().y - Sprite.GetSize().y / 2.0f < Position.y); 
}

sf::Clock menuClock;
float lastTime = menuClock.GetElapsedTime();

bool menu()
{



	bool mOver1 = false, mOver2 = false;
	sf::Event event;
	bool playIntro = false;

	sf::Vector2f mouse_pos(0,0);

	sf::Image start_img, exit_img, bg_img, cursor_img, credits_img, logo_img;
	bg_img.LoadFromFile("data/testowe_tlo.png");
	start_img.LoadFromFile("data/start.png");
	exit_img.LoadFromFile("data/exit.png");
	cursor_img.LoadFromFile("data/menu_cursor.png");
	credits_img.LoadFromFile("data/credits.png");
	logo_img.LoadFromFile("data/logo.png");
	sf::Sprite start, exit, bg, cursor, credits, logo;
	bg.SetImage(bg_img);
	start.SetImage(start_img);
	exit.SetImage(exit_img);
	cursor.SetImage(cursor_img);
	credits.SetImage(credits_img);
	logo.SetImage(logo_img);

	bg.SetCenter(bg_img.GetWidth()/2.0f, 0);
	start.SetCenter(start_img.GetWidth()/2.0f,start_img.GetHeight()/2.0f);
	exit.SetCenter(exit_img.GetWidth()/2.0f,exit_img.GetHeight()/2.0f);
	credits.SetCenter(credits_img.GetWidth()/2.0f,credits_img.GetHeight()/2.0f);
	logo.SetCenter(logo_img.GetWidth()/2.0f,logo_img.GetHeight()/2.0f);
	start.SetPosition(0,430);
	exit.SetPosition(0,580);
	credits.SetPosition(0,700);
	logo.SetPosition(0,180);

	cursor.SetCenter(0,0);

	while(!forceClosed && !playIntro)
	{
		while (app.GetEvent(event))
		{
			if (event.Type == sf::Event::Closed)
                forceClosed = true;
			else if(event.Type == sf::Event::KeyPressed)
			{
				if (event.Key.Code == sf::Key::Escape)
					forceClosed = true;
			}
			else if(event.Type == sf::Event::MouseButtonPressed)
			{
				if(mouseOver(sf::Vector2f(mouse_pos.x,mouse_pos.y),start))
				{
					playIntro = true;
				}
		
				if(mouseOver(sf::Vector2f(mouse_pos.x,mouse_pos.y),exit))
					forceClosed = true;
			}
			else if(event.Type == sf::Event::MouseMoved)
			{
				float windowHeight = app.GetHeight();
				if(windowHeight < 2)
					windowHeight = 1;
				float viewScale = 768.0f/windowHeight;

				mouse_pos.x = (event.MouseMove.X-app.GetWidth()/2.0f)*viewScale;
				mouse_pos.y = event.MouseMove.Y*viewScale;
				
				cursor.SetPosition(mouse_pos.x,mouse_pos.y);
			}
			else if (event.Type == sf::Event::Resized)
			{
				if ((float)event.Size.Width / (float)event.Size.Height > 1800.0 / 768.0f)
				{
					app.SetSize(1800.0 / 768.0f * event.Size.Height, event.Size.Height);
				}
			}
		}/*

		float delta = (menuClock.GetElapsedTime() - lastTime) * 0.01f;
		lastTime = menuClock.GetElapsedTime();

	for (int i = 0 ; i < LEAVE_COUNT ; ++i)
	{
		sf::Vector2f pos = vLeaves[i].Sprite.GetPosition();
		vLeaves[i].Sprite.SetPosition(pos.x + vLeaves[i].Velocity.x + delta,
			pos.y + vLeaves[i].Velocity.y + delta);
		vLeaves[i].fFlyingTime -= delta;
		if (vLeaves[i].fFlyingTime <= 0.0f)
		{
			vLeaves[i].Velocity = sf::Vector2f(-4.0f + 0.1f * (rand() % 40), 1.5f + 0.1f * (rand() % 9));
			vLeaves[i].fFlyingTime = 0.01f * (15 + rand() % 40);
		}
		if (pos.x < -500.0f || pos.y > 768.0f)
		{
			vLeaves[i].Sprite.SetPosition(sf::Vector2f(700.0f - rand() % 1000, -400.0f + rand() % 768));
			vLeaves[i].fScale = 0.5f + 0.1f * (rand() % 6);
			vLeaves[i].fRotSpeed = (rand() % 10) * 0.5f * (rand() ? 1.0f : -1.0f);
		}
	}*/


		if(mouseOver(sf::Vector2f(mouse_pos.x,mouse_pos.y),start))
		{
			start.SetColor(sf::Color(22,255,533));
			if(!mOver1)
			{
				mOver1 = true;
				playSound("click");
			}
		}
		else
		{
			mOver1 = false;
			start.SetColor(sf::Color(255,255,255));
		}

		if(mouseOver(sf::Vector2f(mouse_pos.x,mouse_pos.y),exit))
		{
			exit.SetColor(sf::Color(255,0,255));
			if(!mOver2)
			{
				mOver2 = true;
				playSound("click");
			}
		}
		else
		{
			mOver2 = false;
			exit.SetColor(sf::Color(255,255,255));
		}

		//drawing
		
		app.Clear(sf::Color(32,64,128));

		float windowHeight = app.GetHeight();
		if(windowHeight < 2)
			windowHeight = 1;
		float viewScale = 768.0f/windowHeight;
		float viewX = app.GetWidth()/2.0f*viewScale;
		sf::View view(sf::FloatRect(-viewX, 0, viewX, 768));
		app.SetView(view);
	    
		app.Draw(bg);
		app.Draw(start);
		app.Draw(exit);
		app.Draw(credits);
		app.Draw(logo);


	//for (int i = 0 ; i < LEAVE_COUNT ; ++i)
	//{
	//	vLeaves[i].Sprite.SetColor(sf::Color(255, 255, 255, 200));
	//	vLeaves[i].Sprite.SetScale(vLeaves[i].fScale, vLeaves[i].fScale);
	//	vLeaves[i].Sprite.Rotate(vLeaves[i].fRotSpeed);
	//	app.Draw(vLeaves[i].Sprite);
	//}




		app.Draw(cursor);

		app.Display();


	}

	//intro
	bool step2 = false;

	sf::Image next_img, intro1_img, intro2_img;
	intro1_img.LoadFromFile("data/intro1.png");
	intro2_img.LoadFromFile("data/intro2.png");
	next_img.LoadFromFile("data/next.png");
	sf::Sprite next, intro1, intro2;
	next.SetImage(next_img);
	next.SetCenter(next_img.GetWidth() / 2.0f,next_img.GetHeight() / 2.0f);
	next.SetPosition(300,710);
	
	intro1.SetImage(intro1_img);
	intro1.SetCenter(intro1_img.GetWidth()/2.0f, 0);
	intro2.SetImage(intro2_img);
	intro2.SetCenter(intro2_img.GetWidth()/2.0f, 0);

	while(!forceClosed && playIntro)
	{
		while (app.GetEvent(event))
		{
			if (event.Type == sf::Event::Closed)
                forceClosed = true;
			else if(event.Type == sf::Event::KeyPressed)
			{
				if (event.Key.Code == sf::Key::Escape)
					forceClosed = true;
			}
			else if(event.Type == sf::Event::MouseButtonPressed)
			{
				if(mouseOver(sf::Vector2f(mouse_pos.x,mouse_pos.y),next))
				{
					if(!step2)
						step2 = true;
					else
						playIntro = false;
				}
			}
			else if(event.Type == sf::Event::MouseMoved)
			{
				float windowHeight = app.GetHeight();
				if(windowHeight < 2)
					windowHeight = 1;
				float viewScale = 768.0f/windowHeight;

				mouse_pos.x = (event.MouseMove.X-app.GetWidth()/2.0f)*viewScale;
				mouse_pos.y = event.MouseMove.Y*viewScale;
				
				cursor.SetPosition(mouse_pos.x,mouse_pos.y);
			}
			else if (event.Type == sf::Event::Resized)
			{
				if ((float)event.Size.Width / (float)event.Size.Height > 1800.0 / 768.0f)
				{
					app.SetSize(1800.0 / 768.0f * event.Size.Height, event.Size.Height);
				}
			}
		}

		if(mouseOver(sf::Vector2f(mouse_pos.x,mouse_pos.y),next))
		{
			next.SetColor(sf::Color(22,255,533));
			if(!mOver1)
			{
				playSound("click");
				mOver1 = true;
			}
		}
		else
		{
			mOver1 = false;
			next.SetColor(sf::Color(255,255,255));
		}

		//intro drawing
		app.Clear(sf::Color(32,64,128));

		float windowHeight = app.GetHeight();
		if(windowHeight < 2)
			windowHeight = 1;
		float viewScale = 768.0f/windowHeight;
		float viewX = app.GetWidth()/2.0f*viewScale;
		sf::View view(sf::FloatRect(-viewX, 0, viewX, 768));
		app.SetView(view);
	    
		app.Draw(bg);


		if(step2)
			app.Draw(intro2);
		else
			app.Draw(intro1);

		app.Draw(next);
		app.Draw(cursor);

		app.Display();
	}

	return true;
}